﻿using System;
using System.Web.Mvc;
using Supermodel.DomainModel;
using Supermodel.Exceptions;
using Supermodel.MVC.Extensions;
using Supermodel.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace Supermodel.MVC.FormModels
{
    public class RadioSelectFormModel : DropdownFormModel, IFormModelEditorTemplate
    {
        public enum RadioLayout { Vertical, Horizontal }

        public RadioSelectFormModel()
        {
            Layout = RadioLayout.Vertical;
        }

        public RadioLayout Layout { get; set; }

        new public bool GetIEditorTemplateImplemented() { return true; }
        new public MvcHtmlString EditorTemplate(HtmlHelper html)
        {
            return html.RadioSelectFormModelEditor();
        }
    }

    public class FormModelEnumRadioSelect<EnumT> : RadioSelectFormModel, IFormModelEntityMapper where EnumT : struct, IConvertible
    {
        public class EnumOption : Option
        {
            public EnumOption(EnumT value) : base(value.ToString(), value.GetDescription()) { }
            public EnumT EnumValue
            {
                get
                {
                    return (EnumT)Enum.Parse(typeof(EnumT), Value);
                }
            }
        }

        public FormModelEnumRadioSelect()
        {
            var enumValues = new List<object>();
            foreach (var item in Enum.GetValues(typeof(EnumT))) enumValues.Add(item);
            enumValues = enumValues.OrderBy(x => x.GetScreenOrder()).ToList();

            foreach (var option in enumValues) Options.Add(new EnumOption((EnumT)option));
            SelectedValue = enumValues[0].ToString();
        }

        public EnumT SelectedEnum
        {
            get
            {
                return (EnumT)Enum.Parse(typeof(EnumT), SelectedValue);
            }
            set
            {
                SelectedValue = value.ToString();
            }
        }

        new public void MapFromEntityObject(object obj, Type objType)
        {
            if (objType != typeof(EFCFEnum)) throw new FormModelPropertyCantBeAutomapedException(string.Format("{0} can't be automapped to {1}", GetType().Name, objType.Name));

            var enumObj = (EFCFEnum)obj;
            SelectedEnum = (EnumT)ReflectionHelpers.ExecuteStaticGenericMethod(typeof(EnumExtensions), "GetEnumValue", new[] { (Type)enumObj.PropertyGet("EnumType") }, enumObj);
        }

        new public object MapToEntityObject(object obj, Type objType)
        {
            if (objType != typeof(EFCFEnum)) throw new FormModelPropertyCantBeAutomapedException(string.Format("{0} can't be automapped to {1}", GetType().Name, objType.Name));

            var newEFCFObject = new EFCFEnum();
            ReflectionHelpers.ExecuteStaticGenericMethod(typeof(EnumExtensions), "SetEnumValue", new[] { GetType().GetGenericArguments()[0] }, newEFCFObject, SelectedEnum);
            return newEFCFObject;
        }
    }
}
